package org.luxor.commons.websocket.config;

import org.luxor.commons.websocket.annotation.WebSocketClient;
import org.luxor.commons.websocket.exception.WsClientDeploymentException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * WebSocket客户端注解类处理
 *
 * @author Mr.yan  @date 2021/8/6
 */
public class WsClientProcessor implements BeanPostProcessor {

    private static List<Object> webSocketClientBeans = new ArrayList<>();

    @Nullable
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        WebSocketClient annotation = AnnotationUtils.findAnnotation(bean.getClass(), WebSocketClient.class);
        if (annotation != null) {
            if (StringUtils.isEmpty(annotation.value())) {
                throw new WsClientDeploymentException("Value in @WebSocketClient must not be null ," +
                        "happen in [" + bean.getClass().getName() + "]");
            }
            webSocketClientBeans.add(bean);
        }
        return bean;
    }

    public static List<Object> clientBeans() {
        return webSocketClientBeans;
    }
}
